package com.hgd.security.evp.biz.rest;

import com.alibaba.fastjson.JSONObject;
import com.hgd.security.evp.biz.biz.EGroupBiz;
import com.hgd.security.evp.biz.entity.EvpGroup;
import com.hgd.security.evp.common.util.ObjectResultResponse;
import com.hgd.security.evp.common.util.TableResultResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("eGroup")
@Api(tags = "组管理")
@Log4j
public class EGroupController {
    @Autowired
    private EGroupBiz eGroupBiz;

    @GetMapping("/list/user/{id}")
    @ApiOperation("查询某用户下的组")
    public TableResultResponse<JSONObject> getGroupOfUser(
            @PathVariable(value = "userId") @ApiParam(value = "待查询人员ID") Integer userId
    ){
        return eGroupBiz.getGroupOfUser(userId);
    }

    @PostMapping("/instance")
    @ApiOperation("添加菜单")
    public ObjectResultResponse<Void> addEGroup(@RequestBody @ApiParam(value = "待添加对象") EvpGroup eGroup, BindingResult bindingResult) {
        log.info("/eGroup/instance==>添加菜单");
        ObjectResultResponse<Void> oResult = new ObjectResultResponse<>();
        if (bindingResult.hasErrors()) {
            oResult.setStatus(400);
            oResult.setMessage(bindingResult.getAllErrors().get(0).getDefaultMessage());
            return oResult;
        }

        oResult = eGroupBiz.addEmenu(eGroup);
        return oResult;
    }

    @PutMapping("/instance")
    @ApiOperation("添加菜单")
    public ObjectResultResponse<Void> updateEGroup(@RequestBody @ApiParam(value = "待更新对象") EvpGroup group, BindingResult bindingResult) {
        log.info("/eGroup/instance==>更新菜单");
        ObjectResultResponse<Void> oResult = new ObjectResultResponse<>();
        if (bindingResult.hasErrors()) {
            oResult.setStatus(400);
            oResult.setMessage(bindingResult.getAllErrors().get(0).getDefaultMessage());
            return oResult;
        }

        oResult = eGroupBiz.updateEGroup(group);
        return oResult;
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除单个对象")
    public ObjectResultResponse<Void> delEGroup(@PathVariable("id") @ApiParam(value = "待删除对象ID") Integer id){
        return eGroupBiz.delEGroup(id);
    }

    @GetMapping("/list")
    @ApiOperation("分页获取列表")
    public List<JSONObject> getGroupList(
        @RequestParam(value = "evpCode",defaultValue = "") @ApiParam(value = "组CODE") String evpCode,
        @RequestParam(value = "evpName",defaultValue = "") @ApiParam(value = "组名称") String evpName,
        @RequestParam(value = "page",defaultValue = "1") @ApiParam("当前页，默认为1") Integer page,
        @RequestParam(value = "limit",defaultValue = "10") @ApiParam(value = "页容量，默认为10") Integer limit
    ){
        EvpGroup eGroup=new EvpGroup();
        eGroup.setEvpCode(evpCode);
        eGroup.setEvpName(evpName);

        return eGroupBiz.getGroupList(eGroup,page,limit);
    }
}